close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com




PHP - Basic Operators

Operators are used to perform operations on variables and values


Types of Operator

PHP language supports the following types of operators

  • Arithmetic Operators
  • Assignment Operators
  • Comparison operators
  • Logical operators
  • Increment/Decrement operators
  • String operators
  • Array operators
  • Bitwise Operators

  • Arithmetic Operators

    Arithmetic operators are used with numeric values to perform common mathematical operations


    Operator Name Example
    + Addition $x + $y
    - Subtraction $x - $y
    * Multiplication $x * $y
    / Division $x / $y
    % Modulus $x % $y
    ** Exponentiation $x ** $y

    Assignment Operators

    Assignment operators are used to assign values to variables


    Operator Example Same As
    = x = 7 x = 7
    += x += 6 x = x + 6
    -= x -= 6 x = x - 6
    *= x *= 6 x = x * 6
    /= x /= 6 x = x / 6
    %= x %= 6 x = x % 6



    Comparison Operators

    Comparison operators are used to compare two values


    Operator Name Example Explanation
    == Equal $x == $y Return TRUE if $x is equal to $y
    === Identical $x === $y Return TRUE if $x is equal to $y, and they are of same data type
    !== Not identical $x !== $y Return TRUE if $x is not equal to $y, and they are not of same data type
    != Not equal $x != $y Return TRUE if $x is not equal to $y
    <> Not equal $x <> $y Return TRUE if $x is not equal to $y
    < Less than $x < $y Return TRUE if $x is less than $y
    > Greater than $x > $y Return TRUE if $x is greater than $y
    <= Less than or equal to $x <= $y Return TRUE if $x is less than or equal $y
    >= Greater than or equal to $x >= $y Return TRUE if $x is greater than or equal $y
    <=> Spaceship $x <=>$y Return -1 if $x is less than $y
    Return 0 if $x is equal $y
    Return 1 if $x is greater than $y

    Logical Operators

    Logical operators are used to combine conditional statements


    Operator Name Example Explanation
    and And $x and $y Return TRUE if both $x and $y are true
    Or Or $x or $y Return TRUE if either $x or $y is true
    xor Xor $x xor $y Return TRUE if either $ or $y is true but not both
    ! Not ! $x Return TRUE if $x is not true
    && And $x && $y Return TRUE if either $x and $y are true
    || Or $x || $y Return TRUE if either $x or $y is true

    String Operators

    The string operators are used to perform the operation on strings.


    Operator Name Example Explanation
    . Concatenation $x . $y Concatenate both $x and $y
    .= Concatenation and Assignment $x .= $y First concatenate $x and $y, then assign the concatenated string to $x, e.g. $x = $x . $y

    Array Operators

    The PHP array operators are used to compare arrays.


    Operator Name Example Explanation
    + Union $x + $y Union of $x and $y
    == Equality $x == $y Return TRUE if $x and $y have same key/value pair
    != Inequality $x != $y Return TRUE if $x is not equal to $y
    === Identity $x === $y Return TRUE if $x and $y have same key/value pair of same type in same order
    !== Non-Identity $x !== $y Return TRUE if $x is not identical to $y
    <> Inequality $x <> $y Return TRUE if $x is not equal to $y

    Increment / Decrement Operators

    The PHP increment operators and decrement operators are used to increment and decrement a variable's value.


    Operator Name Description
    ++$x Pre-increment Increments $x by one, then returns $x
    $x++ Post-increment Returns $x, then increments $x by one
    --$x Pre-decrement Decrements $x by one, then returns $x
    $x-- Post-decrement Returns $x, then decrements $x by one

    Bitwise Operators

    Bitwise operators are used to compare (binary) numbers

    Operator Name Description
    AND Sets each bit to 1 if both bits are 1
    | OR Sets each bit to 1 if one of two bits is 1
     ^ XOR Sets each bit to 1 if only one of two bits is 1
    NOT Inverts all the bits
    << Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
    >> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the
    rightmost bits fall off